<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Shop\Api\BannersApi;

class BannersController extends  AdminController{

	public function index(){

		$map = array(
			"parentid" => getDatatree('BANNERS_TYPE'),
		);
        $mapd = array(
            'id'=> '6045'
        );
		$result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($mapd,"","id,name"));
		// dump($result);
		if(!$result['status']) $this->error($result['info']);
		$result = $result['info'];
		$this -> assign('banners_type',$result);
		//
		$type = I('type',0,'int');
		if((!$type || !is_numeric($type))&& !empty($result) && is_array($result)) $type = $result[0]['id'];
		$this -> assign('type',$type);
		if($type){
			$map['position']= $type;
			$params['type'] = $type;
			$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
			$order = " createtime desc ";
			$result = apiCall(BannersApi::QUERY_WITH_POSITION, array($map, $page, $order,$params));
		}else{
			$result = array('status'=>true,'info'=>array('show'=>'','list'=>''));
		}
		//
		if ($result['status']) {
			$this -> assign('show', $result['info']['show']);
			$this -> assign('list', $result['info']['list']);
			$this -> display();
		} else {
			LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
			$this -> error(L('UNKNOWN_ERR'));
		}
	}

	public function add(){
		if(IS_GET){
			$type = I('type',0,'int');
			$this->assign('type',$type);
			$this->display();
		}else{
			$title = I('post.title','');
			//$url =
			$notes    = I('post.notes','');
			$position = I('post.position',18);
			$sort     = I('post.sort',0);
			if(empty($position)){
				$this->error("配置错误！");
			}
			$url_type = $this->_post('url_type',0);
			$entity = array(
				'uid'=>UID,
				'position'=>$position,
				'storeid'=>-1,
				'title'=>$title,
				'notes'=>$notes,
				'img'=>I('img',''),
				'url'=>I('url',''),
				'url_type'=>$url_type,
				'starttime'=>0,
				'endtime'=>0,
				'noticetime'=>0,
				'sort'=>$sort,
			);
			$result = apiCall(BannersApi::ADD, array($entity));
			if(!$result['status']) $this->error($result['info']);
			$this->success("保存成功！",U('Admin/Banners/index',array('type'=>$position)));

		}
	}


	public function edit(){
		$id = I('id',0);
		if(IS_GET){
			$result = apiCall(BannersApi::GET_INFO, array(array('id'=>$id)));
			if(!$result['status']){
				$this->error($result['info']);
			}
			$this->assign('checkedID',$result['info']['url_type']);
			$this->assign("vo",$result['info']);
			$this->display();
		}else{
			$title = I('post.title','');
			//	$url =
			$notes = I('post.notes','');
			$position = I('post.position',18);
			$sort = I('post.sort',0);
			if(empty($position)){
				$this->error("配置错误！");
			}
			$url_type = $this->_post('url_type',0);
			$entity = array(
				'position'=>$position,
				'title'=>$title,
				'notes'=>$notes,
				'img'=>I('post.img',''),
				'url'=>I('post.url',''),
				'url_type'=>$url_type,
				'sort'=>$sort,
			);

			$result = apiCall(BannersApi::SAVE_BY_ID, array($id,$entity));

			if(!$result['status']){
				$this->error($result['info']);
			}
			$this->success("保存成功！",U('Admin/Banners/index',array('type'=>$position)));
		}
	}

	public function delete(){
		$id = I('get.id',0);
		$result = apiCall(BannersApi::DELETE,array(array('id'=>$id)));

		if(!$result['status']){
		    $this->error($result['info']);
		}
		$this->success("删除成功！",U('Admin/Banners/index'));

	}

  //批量删除
  public function delete_all(){
		$tids   = I('post.ids','');
		// dump(I('post.'));exit;
		if($tids == '' || count($tids) == 0){
		    $this -> success('操作成功');
		}
		$result = apiCall(BannersApi::DELETE,array(array('id'=>array('in',$tids))));
		if($result['status']){
		    $this -> success("操作成功");
		}else{
		    $this -> error($result['info']);
		}
  }
}